//
//  QDOrderInfoTableViewCell.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/3/1.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

/// 用于展示订单信息的内容
final class QDOrderInfoView: UIView {
    
    /// 显示 订单编号等
    let titleLabel = UILabel()
    /// 显示真实内容
    let subtitleLabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        titleLabel.text = "订单编号："
        titleLabel.font = Font.light.font(size: 14)
        titleLabel.textColor = QDColors.gray
        
        subtitleLabel.text = "8888888888888888888"
        subtitleLabel.font = Font.regular.font(size: 14)
        subtitleLabel.textColor = QDColors.black
        subtitleLabel.textAlignment = .right
        subtitleLabel.adjustsFontSizeToFitWidth = true
        
        addSubview(titleLabel)
        addSubview(subtitleLabel)
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.top.bottom.equalToSuperview()
        }
        
        subtitleLabel.snp.makeConstraints { (make) in
            make.right.top.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(70).priority(.high)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}



/// 订单详情的cell
final class QDOrderInfoCollectionCell: NWCollectionViewCell {
 
    /// 标题
    var title: String? {
        set {
            infoView.titleLabel.text = newValue
        }get {
            return infoView.titleLabel.text
        }
    }
    
    /// 描述
    var subtitle: String? {
        set {
            infoView.subtitleLabel.text = newValue
        }get {
            return infoView.subtitleLabel.text
        }
    }
    
    /// 详情信息
    let infoView = QDOrderInfoView()
    
    override func buildView() {
        super.buildView()
        
        contentView.addSubview(infoView)
        infoView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
}



/// 扣费详情的信息cell
final class QDOrderInfoTableViewCell: NWTableViewCell {
    
    /// 计算高度
    static func height(count: Int) -> CGFloat {
        var height = 7 + 20 + 22 + 12 + 27 //不包含collectionView的所有高度
        height += 20 * count // cell高度和
        height += 12 * (count - 1) // 间距高度和
        return CGFloat(height)
    }
    
    /// 背景框
    private let backgroundImageView = UIImageView()
    /// 填写订单信息的标签
    let titleLabel = UILabel()
    
    /// 集合视图布局
    lazy private var flowLayout: UICollectionViewFlowLayout = {
        let flowLayout = UICollectionViewFlowLayout()
        
        flowLayout.itemSize = CGSize(width: UIScreen.main.ritl_width - 80, height: 20)
        flowLayout.scrollDirection = .vertical
        flowLayout.minimumLineSpacing = 12
        flowLayout.minimumInteritemSpacing = .leastNormalMagnitude
        
        return flowLayout
    }()
    
    /// 集合视图
    lazy var collectionView: UICollectionView = {
        return UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
    }()
    
    /// 所有的标题
    private var isChanged: Bool = false
    var titles = ["订单编号：","支付方式：","支付时间：","票价总额："] {
        willSet {
            if titles != newValue {
                isChanged = true
            }
        }
    }
    var subtitles = ["888888888","88888888888","888866666","8888877788888","88888888"] {
        willSet {
            if subtitles != newValue {
                isChanged = true
            }
        }
    }
    
    /// 重新刷新
    func reload(){
        if isChanged {
            collectionView.reloadData()
            DispatchQueue.main.async {
                self.isChanged = false//重置
            }
        }
    }
    
    
    override func buildView() {
        super.buildView()
        
        contentView.backgroundColor = RecordBackgroundColor
        backgroundImageView.image = "gen_background".namedImage?.resizableImage(withCapInsets: UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20))
        
        collectionView.backgroundColor = .white
        collectionView.dataSource = self
        collectionView.register(QDOrderInfoCollectionCell.self, forCellWithReuseIdentifier: "cell")
        
        titleLabel.font = Font.semibold.font(size: 16)
        titleLabel.textColor = QDColors.black
        titleLabel.text = "订单信息"
        
        contentView.addSubview(backgroundImageView)
        contentView.addSubview(titleLabel)
        contentView.addSubview(collectionView)
        
        backgroundImageView.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(13)
            make.right.equalToSuperview().inset(13)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(27)
            make.left.equalToSuperview().offset(40)
            make.height.equalTo(22)
        }
        
        
        collectionView.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(12)
            make.left.equalToSuperview().offset(40)
            make.right.equalToSuperview().inset(40)
            make.bottom.equalToSuperview().inset(27)
        }
    }
}


extension QDOrderInfoTableViewCell: UICollectionViewDataSource {
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return titles.count
    }
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        
        if let infoCell = cell as? QDOrderInfoCollectionCell {
            infoCell.title = titles[indexPath.item]
            infoCell.subtitle = subtitles[indexPath.item]
        }
        
        return cell
    }
}
